#include <iostream>
#include <vector>
#include <unordered_map>
#include <algorithm>

using namespace std;
// 49. 字母异位词分组
// 给你一个字符串数组，请你将 字母异位词 组合在一起。可以按任意顺序返回结果列表。
class Solution {
public:
    vector<vector<string>> groupAnagrams(vector<string>& strs) {
        unordered_map<string,vector<string>> map;
        for(string key : strs) {
            string value = key;
            sort(key.begin(),key.end());
            if(map.count(key) == 0) {
                vector<string> s = {value};
                map[key] = s;
            } else {
                map[key].push_back(value);
            }
        }
        vector<vector<string>> res;
        for(pair<string, vector<string>> p : map) {
            res.push_back(p.second);
        }
        return res;
    }
};
void printRes(vector<vector<string>> &res) {
    cout << "[";
    for(int i = 0; i < res.size(); i++) {
        cout << "[";
        for(int j = 0; j < res[i].size(); j++) {
            cout << "\"" << res[i][j] << "\"";
            if(j < res[i].size()-1)
                cout << ",";
        }
        cout << "]";
        if(i < res.size()-1)
            cout << ",";
    }
    cout << "]" << endl;
}

int main() {
    vector<string> strs1 = {"eat", "tea", "tan", "ate", "nat", "bat"};
    vector<vector<string>> res1 = Solution().groupAnagrams(strs1);
    printRes(res1);

    std::cout << "Hello, World!" << std::endl;
    return 0;
}
